package org.csu.mall.controller;

import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.User;
import org.csu.mall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

@Controller
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private IUserService userService;

    @PostMapping("login")
    @ResponseBody
    public UniformResponse<User> login(String username, String password, HttpSession session){
        UniformResponse<User> response = userService.login(username,password);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.LOGIN_USER,response.getData());
        }
        return response;
    }
    @GetMapping("check_username")
    @ResponseBody
    public UniformResponse<String> checkUsername(HttpSession session){
        UniformResponse<String> response = userService.checkUsername();
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.LOGIN_USER,response.getData());
        }
        return response;
    }

    @PostMapping("register")
    @ResponseBody
    public UniformResponse<String> register(User user){
        //添加对于客户端传递来数据的验证
        return userService.register(user);
    }
    @PostMapping("check_valid")
    @ResponseBody
    public UniformResponse<String> checkValid(String type, String value){
        return userService.checkValid(type,value);
    }
    @GetMapping("get_user_info")
    @ResponseBody
    public UniformResponse<User> getUserInfo(HttpSession session){
        UniformResponse<User> response = userService.getUserInfo();
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.USERINFO,response.getData());
        }
        return response;
    }
    @GetMapping("forget_get_question")
    @ResponseBody
    public UniformResponse<String> forgetGetQuestion(String username, HttpSession session){
        UniformResponse<String> response = userService.getForgetQuestion(username);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.USERQUESTION,response.getData());
        }
        return response;
    }
    @PostMapping("forget_check_answer")
    @ResponseBody
    public UniformResponse<String> forgetCheckAnswer(String username, String question,String answer, HttpSession session){
        UniformResponse<String> response = userService.forgetCheckAnswer(username,question,answer);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.CHECKANSWERTOKEN,response.getData());
        }
        return response;
    }
    @PostMapping("forget_reset_password")
    @ResponseBody
    public UniformResponse<String> forgetResetPassword(String username, String newpassword,String forgetToken){
        UniformResponse<String> response = userService.forgetResetPassword(username,newpassword,forgetToken);
        return response;
    }
    @PostMapping("reset_password")
    @ResponseBody
    public UniformResponse<String> resetPassword(String oldpassword, String newpassword){
        UniformResponse<String> response = userService.resetPassword(oldpassword,newpassword);
        return response;
    }
    @PostMapping("update_information")
    @ResponseBody
    public UniformResponse<User> updateInformation(User new_user,HttpSession session){
        UniformResponse<User> response = userService.updateInformation(new_user);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.UPDATEUSER,response.getData());
        }
        return response;
    }
    @GetMapping("logout")
    @ResponseBody
    public UniformResponse<String> logout(){
        UniformResponse<String> response = userService.logout();
        return response;
    }

//    @PostMapping("add_balance")
//    @ResponseBody
//    public UniformResponse<User> addBalance(BigDecimal balance,HttpSession session)
//    {
//        UniformResponse<User> response=userService.addBalance(balance);
//        if(response.isSuccess()){
//            session.setAttribute(CONSTANT.ADDBALANCE,response.getData());
//        }
//        return response;
//    }

}
